<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:系统配置
// +----------------------------------------------------------------------


namespace app\admin\controller;
use app\BackendController;
use app\admin\model\Config as cc;
use think\facade\Cache;
use support\Request;
use think\facade\Db;
use support\Util\Sitemap;

class SystemController extends BackendController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function website()
    {
        $config = new cc();
        $r      = $config->where("option_name", "website")->value('option_value');


        return view("system/website", ['data' => $r]);
    }

    public function saveWebsite(Request $request)
    {
        $config               = new cc();
        $config->option_name  = "website";
        $config->option_value = $request->post();
        $config->replace()->save();
        Cache::delete("configs");
        return json(['status' => 1, 'msg' => "保存成功"]);
    }

    public function email()
    {
        $config = new cc();
        $r      = $config->where("option_name", "email")->value('option_value');
        $r      = $r ? $r : '';
        

        return view("system/email", ['data' => $r]);
    }

    public function saveEmail(Request $request)
    {
        $config               = new cc();
        $config->option_name  = "email";
        $config->option_value = $request->post();
        $config->replace()->save();
        Cache::delete("configs");
        return json(['status' => 1, 'msg' => "保存成功"]);
    }

    public function clearCache()
    {
        Cache::clear();
        return $this->success('清理成功!', '');
    }

    public function filemanage()
    {
        require root_path() . 'extend/filebox.php';
    }

    public function fetchVr()
    {
        $sites = array(
            '720yun'       => '720云全景（频道）',
            '720yunAuthor' => '720云全景(作者)',
            'ivrpano'      => '四方',
            'gy720'        => '光鱼',
            'cnquanjing'   => '中国全景网',
            'krpano100'    => 'krpano100',
            'detu'         => '得图',
            'panorover'    => '全景旅行者',
            '720think'     => '720Think全景云平台',
            'expoon'       => '网展',
            'svsvr'        => '超视景',
            'justeasy'     => '建E',
            'autohome'     => '汽车之家',

        );

        $sites_url = array(
            '720yun'       => 'http://720yun.com/channel',
            '720yunAuthor' => 'http://720yun.com/author',
            'ivrpano'      => 'http://www.ivrpano.com',
            'gy720'        => 'http://www.gy720.com',
            'cnquanjing'   => 'http://www.cnquanjing.com',
            'krpano100'    => 'http://demo.krpano100.com/pictures',
            'detu'         => 'http://www.detu.com/channel/index/13',
            'panorover'    => 'http://panorover.com/SP/Find#!1',
            '720think'     => 'https://www.720think.com/index.php?g=think720&m=home&a=channel',
            'expoon'       => 'https://www.expoon.com/pano/',
            'svsvr'        => 'http://www.svsvr.com/pictures',
            'justeasy'     => 'http://vr.justeasy.cn/panos/606/0/0/0.html',
            'autohome'     => 'http://car.autohome.com.cn/vr/list-0-1.html',

        );
        return view('fetchvr', compact('sites', 'sites_url'));
    }

    public function gatherData()
    {
        $req = new \Collect\fetchVrData();
        $req->gather();
    }

    /**
     * 生成sitemap
     * @return [type] [description]
     */
    public function sitemap(Request $request)
    {
        $domain = 'http://'.$request->host();
        $site = new Sitemap();
        $i    = 0;

        //任务页
        $res = Db::name('mission')->alias("a")->join("mission_description b", "b.mission_id=a.mission_id")->field("a.mission_id,b.title")->select();
        foreach ($res as $v) {
            $i++;
            if ($i > 500) {
                break;
            }

            $site->AddItem($domain . '/' . createUrl('task', $v['title'], $v['mission_id']), 1);
        }
        //中文
        $res = Db::name('info')->alias("a")->join("info_description b", "b.info_id=a.info_id  and b.language_id=1")->field("a.info_id,b.title")->select();
        foreach ($res as $v) {
            $i++;
            if ($i > 500) {
                break;
            }
 
            $site->AddItem($domain . '/' . createUrl('art', $v['title'], $v['info_id']), 1);
        }

        //文章页-英文
        $res = Db::name('info')->alias("a")->join("info_description b", "b.info_id=a.info_id  and b.language_id=2")->field("a.info_id,b.title")->select();
        foreach ($res as $v) {
            $i++;
            if ($i > 500) {
                break;
            }

            $site->AddItem($domain . '/' . createUrl('art', $v['title'], $v['info_id']), 1);
        }

        //首页
        $site->AddItem($domain, 1);

        $site->SaveToFile('sitemap.xml');
        return $this->success('成功生成',"/admin/index/index");

    }
}
